package cn.wuxing.mediation.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.wuxing.mediation.common.exception.ServiceException;
import cn.wuxing.mediation.common.utils.DateUtils;
import cn.wuxing.mediation.common.utils.StringUtils;
import cn.wuxing.mediation.system.domain.ShiftScheduling;
import cn.wuxing.mediation.system.mapper.VisitingReceptionMapper;
import org.springframework.stereotype.Service;
import cn.wuxing.mediation.system.domain.VisitingReception;
import cn.wuxing.mediation.system.service.IVisitingReceptionService;

import javax.annotation.Resource;

/**
 * 考察接待Service业务层处理
 *
 * @author mediation
 * @date 2024-10-12
 */
@Service
public class VisitingReceptionServiceImpl implements IVisitingReceptionService {
    @Resource
    private VisitingReceptionMapper visitingReceptionMapper;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");

    /**
     * 查询考察接待
     *
     * @param id 考察接待主键
     * @return 考察接待
     */
    @Override
    public VisitingReception selectVisitingReceptionById(Long id) {
        return visitingReceptionMapper.selectVisitingReceptionById(id);
    }

    /**
     * 查询考察接待列表
     *
     * @param visitingReception 考察接待
     * @return 考察接待
     */
    @Override
    public List<VisitingReception> selectVisitingReceptionList(VisitingReception visitingReception) {

        String queryEstimatedReceptionTime = visitingReception.getQueryEstimatedReceptionTime();
        if (queryEstimatedReceptionTime != null && (!queryEstimatedReceptionTime.equals(""))) {
            Calendar d = Calendar.getInstance();
            Date now = new Date();
            if (queryEstimatedReceptionTime.contains("-")) {
                visitingReception.setStartTime(queryEstimatedReceptionTime.split("-")[0]);
                visitingReception.setEndTime(queryEstimatedReceptionTime.split("-")[1]);
            } else {
                switch (queryEstimatedReceptionTime) {
                    case "DAY":
                        visitingReception.setEndTime(simpleDateFormat.format(now));
                        now.setHours(0);
                        now.setSeconds(0);
                        now.setMinutes(0);
                        visitingReception.setStartTime(simpleDateFormat.format(now));
                        break;
                    case "WEEK":
                        d.setTime(now);
                        d.add(Calendar.DATE, -7);
                        visitingReception.setStartTime(simpleDateFormat.format(d.getTime()));
                        visitingReception.setEndTime(simpleDateFormat.format(now));
                        break;
                    case "ONEMONTH":
                        d.setTime(now);
                        d.add(Calendar.MONTH, -1);
                        visitingReception.setStartTime(simpleDateFormat.format(d.getTime()));
                        visitingReception.setEndTime(simpleDateFormat.format(now));
                        break;
                    case "SIXMONTH":
                        d.setTime(now);
                        d.add(Calendar.MONTH, -6);
                        visitingReception.setStartTime(simpleDateFormat.format(d.getTime()));
                        visitingReception.setEndTime(simpleDateFormat.format(now));
                        break;
                }
            }
        }
        return visitingReceptionMapper.selectVisitingReceptionList(visitingReception);
    }

    /**
     * 新增考察接待
     *
     * @param visitingReception 考察接待
     * @return 结果
     */
    @Override
    public int insertVisitingReception(VisitingReception visitingReception) {
        visitingReception.setDelFlag("0");
        return visitingReceptionMapper.insertVisitingReception(visitingReception);
    }

    /**
     * 修改考察接待
     *
     * @param visitingReception 考察接待
     * @return 结果
     */
    @Override
    public int updateVisitingReception(VisitingReception visitingReception) {
        visitingReception.setUpdateTime(DateUtils.getNowDate());
        return visitingReceptionMapper.updateVisitingReception(visitingReception);
    }

    /**
     * 批量删除考察接待
     *
     * @param ids 需要删除的考察接待主键
     * @return 结果
     */
    @Override
    public int deleteVisitingReceptionByIds(Long[] ids) {
        return visitingReceptionMapper.deleteVisitingReceptionByIds(ids);
    }

    /**
     * 删除考察接待信息
     *
     * @param id 考察接待主键
     * @return 结果
     */
    @Override
    public int deleteVisitingReceptionById(Long id) {
        return visitingReceptionMapper.deleteVisitingReceptionById(id);
    }

    @Override
    public Map<String, Long> selectVisitingReceptionCount() {
        return visitingReceptionMapper.selectVisitingReceptionCount();
    }

    @Override
    public String importShiftScheduling(List<VisitingReception> visitingReceptionList, boolean updateSupport) {
        if (StringUtils.isNull(visitingReceptionList) || visitingReceptionList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (VisitingReception visitingReception : visitingReceptionList) {
            try {
                // 验证庭室名称是否重复
                if (visitingReception!=null) {
                    if (!visitingReception.getReceptionStatus().equals("")) {
                        if (visitingReception.getReceptionStatus().equals("未接待")) {
                            visitingReception.setReceptionStatus("0");
                        }
                        if(visitingReception.getReceptionStatus().equals("已接待")){
                            visitingReception.setReceptionStatus("1");
                        }
                    }
                    visitingReception.setDelFlag("0");
                    visitingReception.setCreateDate(new Date());
                    visitingReceptionMapper.insertVisitingReception(visitingReception);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单位名称" + visitingReception.getOrganizationName() + " 导入成功");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、单位名称 " + visitingReception.getOrganizationName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
